//
//  LoginViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "LoginViewController.h"
#import "LeftViewController.h"
#import "JASidePanelController.h"
#import "SSKeychain.h"
#import "Users.h"


#define kInputViewWidth 30 //键盘输入的宽度

@interface LoginViewController ()

/**图像*/
@property (weak, nonatomic) IBOutlet UIImageView *iv_icon;

/**用户名*/
@property (weak, nonatomic) IBOutlet UITextField *txf_uername;

/**密码*/
@property (weak, nonatomic) IBOutlet UITextField *txf_password;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (nonatomic, weak) UIView *coverView;

@property (nonatomic, weak) UIView *whiteView;






//方法
- (IBAction)clickLoginButton:(UIButton *)sender;
- (IBAction)clickRegistNewCount:(UIButton *)sender;
- (IBAction)forgetterPassword:(UIButton *)sender;

- (IBAction)clickCheckButtonByPhone:(UIButton *)sender;

@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *iv_input;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.txf_uername addTarget:self action:@selector(judgeTexFiledtInputContent) forControlEvents:UIControlEventEditingChanged];
    [self.txf_password addTarget:self action:@selector(judgeTexFiledtInputContent) forControlEvents:UIControlEventEditingChanged];

    [self setupInputView];
}

#pragma
#pragma mark - 设置输入框
-(void)setupInputView{
    
    self.txf_uername.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kInputViewWidth, 0)];
    self.txf_uername.leftViewMode =   UITextFieldViewModeAlways;
    
    self.txf_password.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kInputViewWidth, 0)];
    self.txf_password.leftViewMode =   UITextFieldViewModeAlways;

    
    [self.view endEditing:YES];
}


#pragma
#pragma mark - 登录
- (IBAction)clickLoginButton:(UIButton *)sender {
    
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:kUsersInfoPath];
    
    [array enumerateObjectsUsingBlock:^(Users * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSString *passwordMD5 = [self.txf_password.text cz_md5String];
        
        if ([passwordMD5 isEqualToString:obj.easymob_password] && [self.txf_uername.text isEqualToString:obj.account])
        {
                [MLDefaults setBool:YES forKey:kIsUserLoginKey];
            
                JASidePanelController *vc = (JASidePanelController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
                [self.navigationController popViewControllerAnimated:YES];
                
                [vc toggleLeftPanel:nil];
                
                _leftViewController.user = obj;  //传递用户数据
            
            [NSKeyedArchiver archiveRootObject:@[obj] toFile:kLoginUserInfoPath];
            
            return ;
                
            
        }else
        {
            
            if (self.coverView == nil)
            {
                UIView *coverView = [[UIView alloc] init];
                
                self.coverView = coverView;
                
                coverView.backgroundColor = [UIColor blackColor];
                
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
                
                [coverView addGestureRecognizer:tap];
                
                coverView.alpha = 0;
                
                UIView *whiteView = [[UIView alloc] init];
                
                whiteView.layer.cornerRadius = 10;
                
                self.whiteView = whiteView;
                
                whiteView.backgroundColor = [UIColor whiteColor];
                
                whiteView.alpha = 0;
                
                [self.view addSubview:coverView];
                [self.view addSubview:whiteView];
                
                UILabel *reminderLabel = [UILabel labelWithTitle:@"温馨提示" andColor:[UIColor blackColor] andFontSize:16];
                reminderLabel.font = [UIFont boldSystemFontOfSize:16];
                
                UILabel *messageLabel = [UILabel labelWithTitle:@"用户名或密码错误" andColor:[UIColor blackColor] andFontSize:14];
                
                [whiteView addSubview:reminderLabel];
                [whiteView addSubview:messageLabel];
                
                [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
                    
                    make.edges.equalTo(self.view);
                }];
                
                [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
                    
                    make.width.mas_equalTo(SCREEN_SIZE.width * 0.8);
                    
                    make.height.mas_equalTo(60);
                    
                    make.center.equalTo(self.view);
                }];
                
                [reminderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    
                    make.centerX.equalTo(whiteView);
                    make.bottom.equalTo(whiteView.mas_centerY).offset(-2);
                    
                }];
                
                [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(whiteView);
                    make.top.equalTo(whiteView.mas_centerY).offset(2);
                }];
                
                
            }
            
            [UIView animateWithDuration:0.3 animations:^{
                
                self.coverView.alpha = 0.5;
                self.whiteView.alpha = 1;
            }];
        }
        
        return ;
        
    }];

    
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.3 animations:^{
        self.coverView.alpha = 0;
        self.whiteView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.coverView removeFromSuperview];
        [self.whiteView removeFromSuperview];
    }];
}

#pragma mark -
#pragma mark - judgeTexFiledtInputContent
//判断文本文件的输入内容
- (void)judgeTexFiledtInputContent
{
    if(self.txf_uername.text.length > 0 && self.txf_password.text.length > 0)
    {
        self.loginBtn.enabled = YES;
    }
    else
    {
        self.loginBtn.enabled = NO;
        
    }
}



#pragma
#pragma mark - 注册账号
- (IBAction)clickRegistNewCount:(UIButton *)sender {
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"RegisterViewController" bundle:nil];
    UIViewController *vc = [sb instantiateInitialViewController];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma
#pragma mark - 忘记密码
- (IBAction)forgetterPassword:(UIButton *)sender {
    
    
}

#pragma
#pragma mark - 手机验证码
- (IBAction)clickCheckButtonByPhone:(UIButton *)sender {
    
    
}

- (void)dealloc
{
    NSLog(@"登录控制器死了");
}
@end
